package org.unt.exceptions;

public class Exceptions {

	public static String toString(Throwable e) {

		StringBuffer buf = new StringBuffer();

		buf.append(toStr(e));

		Throwable cause = e.getCause();

		while (cause != null) {
			buf.append((Exceptions.causeToString(cause)));
			cause = cause.getCause();
		}

		return buf.toString();
	}

	private static String toStr(Throwable e) {

		StringBuffer buf = new StringBuffer(e.getClass() + " : " + e.getMessage());

		StackTraceElement[] stack = e.getStackTrace();

		for (int i = 0; i < stack.length; i++) {
			buf.append("\n\tat " + stack[i].toString());
		}

		return buf.toString();
	}

	public static String causeToString(Throwable e) {
		return "\nCAUSE\n" + toStr(e);
	}

	public static String causeToString(Exception e) {
		return "\nCAUSE\n" + toStr(e);
	}
}
